package com.ctshk.rpc.hotel.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.rpc.hotel.dto.HotelProductListDTO;
import com.ctshk.rpc.hotel.dto.HotelProductManagePageDTO;
import com.ctshk.rpc.hotel.dto.HotelProductStrategyDTO;
import com.ctshk.rpc.hotel.entity.HotelProductStrategy;
import com.ctshk.rpc.hotel.req.HotelListProductReq;
import com.ctshk.rpc.hotel.req.HotelProductPageReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 酒店产品加幅策略 Mapper 接口
 * </p>
 *
 * @author yinhuan
 * @since 2021-03-03
 */
public interface HotelProductStrategyMapper extends BaseMapper<HotelProductStrategy> {
/**
 * 产品中心-获取巴士票产品分页列表
 * @param req
 * @return
 */
//    @Select("<script>"+
//            "SELECT t_hotel.city_name,t_hotel.name,t_hotel_room_type.name,t_hotel_product.name,t_hotel_room_type.bed_type," +
//            "t_hotel_product.has_breakfast,t_hotel_product.source_type,t_hotel_product.supplier_name,t_hotel_product.target_population," +
//            "t_hotel_product.allow_package,t_hotel_product.on_shelf_status,t_hotel_product.recommendation_status" +
//            " from t_hotel,t_hotel_room_type,t_hotel_product \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 0\">" +
//            "left join t_hotel_product_channel_online_self_platform  sp on a.id  = sp.increase_strategy_id \n"+
//            "left join t_hotel_product_channel_online_third_platform  tp on a.id  = tp.increase_strategy_id \n"+
//            "left join t_hotel_product_channel_offline_self_custom  sc on a.id  = sc.increase_strategy_id \n"+
//            "left join t_hotel_product_channel_offline_agent_custom  ac on a.id  = ac.increase_strategy_id"+
//            "</if> \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 1\">" +
//            "left join t_hotel_product_channel_online_self_platform  sp on a.id  = sp.increase_strategy_id \n"+
//            "left join t_hotel_product_channel_online_third_platform  tp on a.id  = tp.increase_strategy_id" +
//            "</if> \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 2\">" +
//            "left join t_hotel_product_channel_online_third_platform  tp on a.id  = tp.increase_strategy_id" +
//            "</if> \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 3\">" +
//            "left join t_hotel_product_channel_online_self_platform  sp on a.id  = sp.increase_strategy_id" +
//            "</if> \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 4\">" +
//            "left join t_hotel_product_channel_offline_self_custom  sc on a.id  = sc.increase_strategy_id \n"+
//            "left join t_hotel_product_channel_offline_agent_custom  ac on a.id  = ac.increase_strategy_id" +
//            "</if> \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 5\">" +
//            "left join t_hotel_product_channel_offline_self_custom  sc on a.id  = sc.increase_strategy_id" +
//            "</if> \n" +
//
//            "<if test=\"req.channelType != null and req.channelType == 6\">" +
//            "left join t_hotel_product_channel_offline_agent_custom  ac on a.id  = ac.increase_strategy_id" +
//            "</if> \n" +
//            "where\n" +
//            "\tt_hotel.ID = t_hotel_room_type.hotel_id\n" +
//            "and\n" +
//            "\tt_hotel_room_type.ID = t_hotel_product.room_id"+
//            " and t_hotel_product.is_deleted = 0"+
//            "<if test=\"req.cityName != null and req.cityName != ''\">AND t_hotel.city_name LIKE CONCAT('%',#{req.cityName},'%')</if> \n" +
//            "<if test=\"req.brandName != null and req.brandName != ''\">AND t_hotel.brand_name LIKE CONCAT('%',#{req.brandName},'%')</if> \n" +
//            "<if test=\"req.hotelName != null and req.hotelName != ''\">AND t_hotel.name LIKE CONCAT('%',#{req.hotelName},'%')</if> \n" +
//            "<if test=\"req.roomName != null and req.roomName != ''\">AND t_hotel_room_type.room_name LIKE CONCAT('%',#{req.roomName},'%')</if> \n" +
//            "<if test=\"req.sourceType != null and req.sourceType != 0\">AND t_hotel_product.source_type = #{req.sourceType})</if> \n" +
//            "<if test=\"req.supplierName != null and req.supplierName != ''\">AND t_hotel_product.supplier_name LIKE CONCAT('%',#{req.supplierName},'%')</if> \n" +
//            "<if test=\"req.recommendationStatus != null and req.recommendationStatus != 0\">AND b.recommendation_status = #{req.recommendationStatus})</if> \n" +
//            "</script>")
//    IPage<HotelProductManagePageDTO> queryList(@Param("req") HotelProductPageReq req, IPage<HotelProductManagePageDTO> agentPage);
    @Select("<script>"+"SELECT\n" +
            "\tt_hotel.id,\n" +
            "\tt_hotel_product.id as productId,\n" +
            "\tt_hotel_room_type.id as roomId,\n" +
            "\tt_hotel.city_name,\n" +
            "\tt_hotel.brand_name,\n" +
            "\tt_hotel.name as hotelName,\n" +
            "\tt_hotel_room_type.NAME as roomName,\n" +
            "\tt_hotel_product.NAME as productName,\n" +
            "\tt_hotel_room_type.bed_type,\n" +
            "\tt_hotel_product.has_breakfast as breakfast,\n" +
            "\tt_hotel.source_type as sourceType,\n" +
            "\tt_hotel_product.supplier_name,\n" +
            "\tt_hotel_product.target_population,\n" +
            "\tt_hotel_product.charge_user as chargerUser,\n" +
            "\tt_hotel_product.sale_status as saleStatus,\n" +
            "\tt_hotel_product.allow_package as allowPackage, \n" +
            "\tt_hotel_product.on_shelf_status as onShelfStatus \n" +
            "\tt_hotel_product.recommendation_status as recommendationStatus \n" +
            "FROM\n" +
            "\tt_hotel,\n" +
            "\tt_hotel_room_type,\n" +
            "\tt_hotel_product\n" +
            "where\n" +
            "\tt_hotel.id = t_hotel_room_type.hotel_id \n" +
            "and \n" +
            "  t_hotel.id = t_hotel_product.hotel_id\n" +
            "and\n" +
            "\tt_hotel_room_type.id = t_hotel_product.room_id\n" +
            "and 1=1"+
            "<if test=\"req.cityName != null and req.cityName != ''\"> and (t_hotel.city_name LIKE CONCAT('%',#{req.cityName},'%'))</if> \n"+
            "<if test=\"req.brandName != null and req.brandName != ''\"> and (t_hotel.brand_name LIKE CONCAT('%',#{req.brandName},'%'))</if> \n"+
            "<if test=\"req.hotelName != null and req.hotelName != ''\"> and (t_hotel.name LIKE CONCAT('%',#{req.hotelName},'%'))</if> \n"+
            "<if test=\"req.roomName != null and req.roomName != ''\"> and (t_hotel_room_type.name LIKE CONCAT('%',#{req.roomName},'%'))</if> \n"+
            "<if test=\"req.supplierName != null and req.supplierName != ''\"> and (t_hotel_product.supplier_name LIKE CONCAT('%',#{req.supplierName},'%'))</if> \n"+
            "<if test=\"req.sourceType != null and req.sourceType != ''\"> and t_hotel_product.source_type = #{req.sourceType}</if> \n"+
            "<if test=\"req.name != null and req.name != ''\"> and t_hotel_product.name = #{req.name}</if> \n"+
            "<if test=\"req.onShelfStatus != null and req.onShelfStatus != ''\"> and t_hotel_product.on_shelf_status = #{req.onShelfStatus}</if> \n"+
            "<if test=\"req.recommendationStatus != null and req.recommendationStatus != ''\"> and t_hotel_product.recommendation_status = #{req.recommendationStatus}</if> \n"
            +"</script>")
    Page<HotelProductListDTO> queryHotelProductList(HotelListProductReq req, Page<HotelProductListDTO> page);



    /**
     * 根据产品id和日期查询策略
     */
    @Select("<script>"+"select a.id,a.increase_strategy_name," +
            "a.self_sale_permission_type," +
            "a.agent_sale_permission_type,a.self_sale_permission_id," +
            "a.agent_sale_permission_id " +
            "from t_hotel_product_strategy a \n" +
            "left join t_hotel_product b on a.hotel_product_id  = b.id \n" +
            "left join t_hotel_product_strategy_date c on a.id = c.increase_strategy_id \n" +
            "WHERE b.is_deleted = 0 \n" +
            "AND c.date_begin &lt;=#{queryDate} and c.date_end &gt;=#{queryDate}\n" +
            "AND b.id =#{productId} \n" +
            "<if test=\"req.cityName != null and req.cityName != ''\"> and (t_hotel.city_name LIKE CONCAT('%',#{req.cityName},'%'))</if> \n"+
            "<if test=\"req.brandName != null and req.brandName != ''\"> and (t_hotel.brand_name LIKE CONCAT('%',#{req.brandName},'%'))</if> \n"+
            "<if test=\"req.name != null and req.name != ''\"> and t_hotel_product.name = #{req.name}</if> \n"+
            "group by b.id limit 1\n" +
            "</script>")
    HotelProductStrategyDTO queryHotelProductStrategyInfo(@Param("productId") Long productId, @Param("queryDate") LocalDate queryDate);


    @Select({"<script>",
            "select id from t_hotel_product_strategy where 1=1",
            "and hotel_product_id in <foreach collection='list' item='item' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<Long> selectIdListByHotelProductIds(List<Long> hotelProductIds);

    @SelectProvider(value = HotelProductIncreaseStrategyProvider.class, method = "selectByHotelProductIdNowDate")
    HotelProductStrategy selectByHotelProductIdNowDate(@Param("hotelProductId") Long hotelProductId, @Param("nowDate") String nowDate);

    @Select({"<script>",
            "select aeis.agent_sale_permission_type " +
                    "from t_hotel_product_strategy_date aeisd " +
                    "LEFT JOIN (select taeis.id, taeis.agent_sale_permission_type from t_hotel_product_strategy taeis where taeis.hotel_product_id = #{id}) aeis " +
                    "ON aeisd.increase_strategy_id = aeis.id " +
                    "where #{date} BETWEEN aeisd.date_begin and aeisd.date_end" +
                    " and aeis.agent_sale_permission_type is not null limit 1;",
            "</script>"})
    Integer selectAgentSaleTypeByIdDate(@Param("id") Long id, @Param("date")LocalDate now);
}
